<script type="text/javascript">
<!--
$(document).ready(function(){
    $('#btn_discount_all').click(function (){
        var d = prompt('Nhập số % giảm giá áp dụng cho tất cả các danh mục ?', 0);
        if (d != null){
            $.post(BASE_URL + "admin/category/discount_all", {discount: d}, function(data){
                if (data == true){
                    alert('Thay đổi giảm giá danh mục thành công.');
                    window.location.reload();
                }
            });
        }
    });
    $('.del_category').click(function(){
        if (!confirm('Bạn có chắc chắn muốn xóa không ?')){
            return false;
        }

        i = $(this);
        $.post(ADMIN_URL + '/category_delete', {id: i.attr('_id')}, function(data){
            if (data == true){
                i.parent().parent().remove();
            }
        });
    });
});
//-->
</script>
<div class="content_wrapper">
    <legend>Danh mục sản phẩm <span id="btn_discount_all" style="color: red; float: right; font-size: 12px; font-weight: normal; cursor: pointer;">Thay đổi giảm giá tất cả danh mục</span></legend>
    <table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered table-condensed" id="example" width="100%">
        <thead>
            <tr>
                <th width="25" style="text-align: center;">ID</th>
                <th>Tên danh mục</th>
                <th width="100" style="text-align: center;">Giảm giá (%)</th>
                <th width="40" style="text-align: center;">Home</th>
                <th width="40" style="text-align: center;">Active</th>
                <th width="60">Tùy chọn</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach($result as $key => $value) :?>
                <?php if ($value['parent_id'] == 0) :?>
                 <tr>
                    <td style="text-align: center;"><?php echo $value['category_id'];?></td>
                    <td><?php echo $value['title'];?></td>
                    <td style="text-align: center;"><?php echo $value['discount'];?></td>
                    <td style="text-align: center;"><i _id_key="category_id" _id_value="<?php echo $value['category_id'];?>" _field_key="home_flag" _field_value="<?php echo $value['home_flag'];?>" _table="<?php echo TBL_PRODUCT_CATEGORY;?>" _icon_active="icon-ok" _icon_unactive="icon-ban-circle" class="btn_active <?php echo ($value['home_flag'] == 1) ? 'icon-ok' : 'icon-ban-circle'; ?>"></i></td>
                    <td style="text-align: center;"><i _id_key="category_id" _id_value="<?php echo $value['category_id'];?>" _field_key="active_flag" _field_value="<?php echo $value['active_flag'];?>" _table="<?php echo TBL_PRODUCT_CATEGORY;?>" _icon_active="icon-ok" _icon_unactive="icon-ban-circle" class="btn_active <?php echo ($value['active_flag'] == 1) ? 'icon-ok' : 'icon-ban-circle'; ?>"></i></td>
                    <td style="text-align: center;">
                        <a href="<?php echo base_url('admin/category/update/'.$value['category_id']);?>"><i class="icon-edit"></i></a>
                        <a href="javscript: void(0);" _id="<?php echo $value['category_id'];?>" class="del_category"><i class="icon-remove"></i></a>
                    </td>
                </tr>
                <?php endif;?>

                <?php foreach($result as $sub_key => $sub_value) :?>
                <?php if ($value['category_id'] == $sub_value['parent_id']) :?>
                 <tr>
                    <td style="text-align: center;"><?php echo $sub_value['category_id'];?></td>
                    <td> ----- <?php echo $sub_value['title'];?></td>
                    <td style="text-align: center;"><?php echo $sub_value['discount'];?></td>
                    <td style="text-align: center;"><i _id_key="category_id" _id_value="<?php echo $sub_value['category_id'];?>" _field_key="home_flag" _field_value="<?php echo $sub_value['home_flag'];?>" _table="<?php echo TBL_PRODUCT_CATEGORY;?>" _icon_active="icon-ok" _icon_unactive="icon-ban-circle" class="btn_active <?php echo ($sub_value['home_flag'] == 1) ? 'icon-ok' : 'icon-ban-circle'; ?>"></i></td>
                    <td style="text-align: center;"><i _id_key="category_id" _id_value="<?php echo $sub_value['category_id'];?>" _field_key="active_flag" _field_value="<?php echo $sub_value['active_flag'];?>" _table="<?php echo TBL_PRODUCT_CATEGORY;?>" _icon_active="icon-ok" _icon_unactive="icon-ban-circle" class="btn_active <?php echo ($sub_value['active_flag'] == 1) ? 'icon-ok' : 'icon-ban-circle'; ?>"></i></td>
                    <td style="text-align: center;">
                        <a href="<?php echo base_url('admin/category/update/'.$sub_value['category_id']);?>"><i class="icon-edit"></i></a>
                        <a href="javscript: void(0);" _id="<?php echo $sub_value['category_id'];?>" class="del_category"><i class="icon-remove"></i></a>
                    </td>
                </tr>
                <?php endif;?>
                <?php endforeach;?>
            <?php endforeach;?>
        </tbody>
    </table>
</div>
